package com.zdlee.netty.server19.server.handler;

import com.zdlee.netty.server19.protocol.request.GroupMessageRequestPacket;
import com.zdlee.netty.server19.protocol.response.GroupMessageResponsePacket;
import com.zdlee.netty.server19.util.SessionUtil;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.group.ChannelGroup;

/**
 * @author zdlee
 * @description: ${DESCRIPTION}
 * @create 2018-12-03 16:35
 */
@ChannelHandler.Sharable
public class GroupMessageRequestHandler extends SimpleChannelInboundHandler<GroupMessageRequestPacket> {

    public static final GroupMessageRequestHandler INSTANCE=new GroupMessageRequestHandler();

    private GroupMessageRequestHandler(){

    }


    @Override
    protected void channelRead0(ChannelHandlerContext ctx, GroupMessageRequestPacket msg) throws Exception {
        // 1.拿到 groupId 构造群聊消息的响应
        String groupId=msg.getToGroupId();
        GroupMessageResponsePacket responsePacket=new GroupMessageResponsePacket();
        responsePacket.setFromGroupId(groupId);
        responsePacket.setMessage(msg.getMessage());
        responsePacket.setFromUser(SessionUtil.getSession(ctx.channel()));

        //2.拿到群聊对应的channelGroup,写到每个客户端
        ChannelGroup channelGroup=SessionUtil.getChannelGroup(groupId);
        channelGroup.writeAndFlush(responsePacket);

    }
}
